/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package za.co.pas.business.model.ext;

import java.beans.PropertyChangeListener;
import za.co.pas.business.abstracts.AbstractDataModel;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import javax.persistence.EntityExistsException;
import org.apache.log4j.Logger;
import za.co.pas.business.message.RequestCompanyDetailChange;
import za.co.pas.business.data.Address;
import za.co.pas.business.data.Company;
import za.co.pas.business.data.UserLoginDetails;
import za.co.pas.business.data.audit.Address_AUDIT;
import za.co.pas.business.data.audit.Company_AUDIT;
import za.co.pas.business.data.audit.UserLoginDetails_AUDIT;
import za.co.pas.business.message.CompanyDetailChanged;
import za.co.pas.business.message.CompanyListChanged;
import za.co.pas.business.message.CompanySaved;
import za.co.pas.business.model.CompanyDataModel;

/**
 *
 * @author alabuschagne
 */
public class CompanyDetailModel extends AbstractDataModel
{
    private Company currentCompany;  
    private CompanyDataModel companyDataModel = new CompanyDataModel();
    private static Logger logger = Logger.getLogger(CompanyDetailModel.class);
    public CompanyDetailModel()
    {
        super();
    }

    private void fireCompanyChanged()
    {
        firePropertyChange(new CompanyDetailChanged(currentCompany));
    }    

    private void makeNewCompany()
    {
        currentCompany = new Company();
        currentCompany.setPostalAddress(new Address());
        currentCompany.setStreetAddress(new Address());
        fireCompanyChanged();
    }

    public void setSaveCompany(Company newCompany, UserLoginDetails currentUser)
    {
        currentCompany.logPrint(0);
        if(currentCompany.set(newCompany, currentUser))
        {
            currentCompany.logPrint(0);
            if(currentCompany.getId() != null)
            {
                companyDataModel.mergseGenericObject(currentCompany);
            }
            else
            {
                companyDataModel.persistGenericObject(currentCompany);
            }
            List<Company_AUDIT> companyAuditList = currentCompany.getCompanyAuditList();
            ListIterator<Company_AUDIT> itCal = companyAuditList.listIterator();
            
            List<Address_AUDIT> addressAuditListS = null;
            List<Address_AUDIT> addressAuditListP = null;
            List<UserLoginDetails_AUDIT> userAuditList = null;
            if(currentCompany.getStreetAddress() != null)
            {
                addressAuditListS = currentCompany.getStreetAddress().getAddressAuditList();
                
            }
            if(currentCompany.getPostalAddress() != null)
            {
                addressAuditListP = currentCompany.getPostalAddress().getAddressAuditList();
                
            }
            if(currentCompany.getUsers() != null)
            {
                userAuditList = new LinkedList<UserLoginDetails_AUDIT>();
                ListIterator<UserLoginDetails> it = currentCompany.getUsers().listIterator();
                while(it.hasNext())
                {
                    UserLoginDetails u = it.next();
                    ListIterator<UserLoginDetails_AUDIT> it2 = u.getUserAuditList().listIterator();
                    while(it2.hasNext())
                    {
                        userAuditList.add(it2.next());
                    }
                }
                
            }

            if((companyAuditList != null) && (!companyAuditList.isEmpty()))
            {
                try
                {
                    persistGenericList(companyAuditList);
                }
                catch(EntityExistsException e)
                {
                    //@todo handle error!
                }
            }
            if((addressAuditListS != null) && (!addressAuditListS.isEmpty()))
            {
                try
                {
                    persistGenericList(addressAuditListS);
                }
                catch(EntityExistsException e)
                {
                    //@todo handle error!
                }
            }
            if((addressAuditListP != null) && (!addressAuditListP.isEmpty()))
            {
                try
                {
                    persistGenericList(addressAuditListP);
                }
                catch(EntityExistsException e)
                {
                    //@todo handle error!
                }
            }
            if((userAuditList != null) && (!userAuditList.isEmpty()))
            {
                try
                {
                    persistGenericList(userAuditList);
                }
                catch(EntityExistsException e)
                {
                    //@todo handle error!
                }
            }
            firePropertyChange(new CompanySaved(currentCompany));
        }
    }

   

    /**
     * NB: View needs to call setRequestCurrentCompany first. View needs to decide if this can be set.
     * @param company to be set as the current company
     */
    public void setCurrentCompany(Company company)
    {
        if(company == null)
        {
            //This is an error!
        }
        else
        {
            currentCompany = company;
            firePropertyChange(new CompanyDetailChanged(currentCompany));
        }
    }

    /**
     * Call this first to load or create company
     * @param company The next company to change to.
     */
    public void setRequestCurrentCompany(Company company)
    {
        firePropertyChange(new RequestCompanyDetailChange(currentCompany, company));
    }

    public List<Company> getCompanies(boolean b)
    {
        return companyDataModel.getCompanies(b);
    }

    public void setRequestCompanyList(Boolean b)
    {
        firePropertyChange(new CompanyListChanged(getCompanies(b)));
    }

    public Company getCurrentCompany()
    {
        return currentCompany;
    }

    public String makeCompanyCode(String name)
    {
        return companyDataModel.makeCompanyCode(name);
    }

     @Override
    public void addPropertyChangeListener(PropertyChangeListener listener)
    {
        propertyChangeSupport.addPropertyChangeListener(listener);
        companyDataModel.addPropertyChangeListener(listener);
    }

    @Override
    public void removePropertyChangeListener(PropertyChangeListener listener)
    {
        propertyChangeSupport.removePropertyChangeListener(listener);
        companyDataModel.removePropertyChangeListener(listener);
    }

}
